package com.bole.controller;

import com.bole.data.Result;
import com.bole.dto.collections.CollectionsDto;
import com.bole.dto.user.UserDto;
import com.bole.feign.reception.CollectionsFeign;
import com.bole.feign.reception.UserServiceFeign;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
@CrossOrigin
@RestController
@RequestMapping("collections")
public class CollectionsController {
    @Resource
    private CollectionsFeign collectionsFeign;

    @Resource
    private UserServiceFeign userServiceFeign;
    /**
     * 保存
     * @return
     */
    @RequestMapping("saveCollections")
    public Result saveCollections(Long carId, String telePhone) {
        UserDto userDto = userServiceFeign.getUserByTelePhone(telePhone);
        CollectionsDto collectionsDto = new CollectionsDto();
        collectionsDto.setCarId(carId);
        collectionsDto.setUserId(userDto.getId());
        Result result = collectionsFeign.saveCollections(collectionsDto);
        return result;
    }

    /**
     * 判断是否存在收藏
     * @param carId
     * @param telePhone
     * @return
     */
    @CrossOrigin
    @GetMapping("/isCollection/{carId}/{telePhone}")
    public Result isCollection(@PathVariable("carId") Long carId,@PathVariable("telePhone") String telePhone) {
        System.out.println(telePhone);
        UserDto userDto = userServiceFeign.getUserByTelePhone(telePhone);
        Result result = collectionsFeign.isCollection(carId, userDto.getId());
        return result;
    }

    /**
     * 获取收藏
     * @return
     */
    @GetMapping("/findCollection")
    public Result findCollection() {
        Subject subject = SecurityUtils.getSubject();
        UserDto userDto = (UserDto) subject.getPrincipal();
        Result result = collectionsFeign.findCollection(userDto.getId());
        return result;
    }
    @DeleteMapping("/deleteCollections/{carId}")
    public Result deleteCollections(@PathVariable("carId") Long carId) {
        Subject subject = SecurityUtils.getSubject();
        UserDto userDto = (UserDto) subject.getPrincipal();
        Result result = collectionsFeign.deleteCollections(carId,userDto.getId());
        return result;
    }
}
